package cn.terys.terystransactionclient;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.MathUtilities;

public class RefundActivity extends BaseActivity
{

    public static final String INTENT_EXTRANAME_ORDERNUMBER = "ORDERNUMBER";
    public static final String INTENT_EXTRANAME_SUM         = "SUM";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!WelcomeActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, WelcomeActivity.class);
            return;
        }
        setContentView(R.layout.refund);
        setTitle(R.string.refund_title);

        final EditText orderNum = (EditText) findViewById(R.id.order_num);
        final EditText sum = (EditText) findViewById(R.id.sum);
        sum.setSelection(sum.getText().length());
        sum.addTextChangedListener(new TextWatcher()
        {
            boolean      shouldChange = true;
            CharSequence realText     = "";

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                // TODO Auto-generated method stub
                if (shouldChange)
                {
                    if (before > 0)
                    {
                        int length = realText.length();
                        if (length > 0)
                            realText = realText.subSequence(0, length - 1);
                    } else
                    {
                        realText = realText + s.subSequence(start, start + count).toString();
                        if (realText.equals("0"))
                            realText = "";
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                // TODO Auto-generated method stub
                if (shouldChange)
                {
                    int length = realText.length();
                    String sumStr = null;
                    if (length == 0)
                        sumStr = "0.00";
                    else if (length == 1)
                        sumStr = "0.0" + realText;
                    else if (length == 2)
                        sumStr = "0." + realText;
                    else
                    {
                        CharSequence start = realText.subSequence(0, length - 2);
                        CharSequence end = realText.subSequence(length - 2, length);
                        sumStr = start + "." + end;
                    }
                    shouldChange = false;
                    sum.setText(sumStr);
                    sum.setSelection(sumStr.length());
                    shouldChange = true;
                }
            }
        });

        Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String orderStr = orderNum.getText().toString();
                String sumStr = sum.getText().toString();
                if (TextUtils.isEmpty(orderStr))
                {
                    ToastManager.showLong(RefundActivity.this, R.string.refund_confirm_orderempty);
                    return;
                }
                if (sumStr.equals("0.00"))
                {
                    ToastManager.showLong(RefundActivity.this, R.string.refund_confirm_sumerror);
                    return;
                }
                sumStr = MathUtilities.round2(MathUtilities.mul(Double.parseDouble(sumStr), 100), 0);
                Intent intent = new Intent(RefundActivity.this, RefundConfirmActivity.class);
                intent.putExtra(INTENT_EXTRANAME_ORDERNUMBER, orderStr);
                intent.putExtra(INTENT_EXTRANAME_SUM, sumStr);
                startActivity(intent);
            }
        });
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });

    }

}